<template>
    <el-dialog
      :title="title"
      :visible.sync="openDlg"
      width="500px"
      append-to-body
      destroy-on-close
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="90px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="注册号" prop="registerCode">
                <el-input v-model.trim="form.registerCode" @change="registerCodeChange" placeholder="请输入注册号" />
            </el-form-item>
          </el-col>
          <el-col :span="24" >
            <el-form-item label="学院" v-if="scholarshipTypeArr.some((item) => { return item.code == userType})" prop="collegeId">
              <el-select v-model="form.collegeId" :disabled="true" style="width: 100%;" placeholder="请选择">
                <el-option
                  v-for="item in collegeList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="学院" v-if="userType == 'subsidy' || userType == 'schoolScholarship'" prop="collageId">
              <el-select v-model="form.collageId" :disabled="true" style="width: 100%;" placeholder="请选择">
                <el-option
                  v-for="item in collegeList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24" >
            <!-- <el-form-item label="国奖类型" v-if="userType == 'scholarship'" prop="rewardType">
              <el-select v-model="form.rewardType" :disabled="true" style="width: 100%;" placeholder="请选择">
                <el-option
                  v-for="item in rewardTypeArr"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item> -->
            <el-form-item label="困难生类型" v-if="userType == 'subsidy'" prop="diffTypeId">
              <el-select v-model="form.diffTypeId" :disabled="true" style="width: 100%;" placeholder="请选择">
                <el-option
                  v-for="item in subsidyType"
                  :key="item.typeId"
                  :label="item.diffName"
                  :value="item.typeId">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="奖学金类型" v-if="userType == 'schoolScholarship'" prop="typeName">
              <el-select v-model="form.typeName" :disabled="true" style="width: 100%;" placeholder="请选择">
                <el-option
                  v-for="item in subsidyType"
                  :key="item"
                  :label="item"
                  :value="item">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="openDlg = false">取 消</el-button>
      </div>
    </el-dialog>
</template>

<script>
import { 
   editScholarshipUser,
   editPoorStudentUser,
   schoolScholarshipUserEdit
  } from "@/api/poorStudent-scholarship/user.js";
import { 
  queryStudent
  } from "@/api/information/studentInfor.js";
import {
    scholarshipTypeArr
} from "@/views/poorStudent-scholarship/dict.js"
export default {
    name:"userEdit",
    props:{
      subsidyType:{
        type:Array,
        default: ()=>{return []}
      },
      collegeList:{
        type:Array,
        default: ()=>{return []}
      }
    },
    data () {
        return {
            title: '修改',
            openDlg: false,
            // 表单参数
            form: {},
            // 表单校验
            rules: {
              collegeId: [
                { required: true, message: '请选择学院', trigger: 'change' }
              ],
              collageId: [
                { required: true, message: '请选择学院', trigger: 'change' }
              ],
              rewardType: [
                { required: true, message: '请选择国奖类型', trigger: 'change' }
              ],
              registerCode: [
                { required: true, message: '请输入注册号', trigger: 'blur' }
              ],
            },
            rewardTypeArr:[{
                label: "国家奖学金",
                value: 1
            },{
                label: "国家励志奖学金",
                value: 2
            }],
            userType: 'scholarship',
            scholarshipTypeArr
        }
    },
    methods:{
        open (row, type) {
          this.userType = type
          this.form = {...row}
          this.openDlg = true
        },
        registerCodeChange (val) {
          if (val) {
            queryStudent({
              registerOrSno: val
            }).then((response) => {
              if (this.scholarshipTypeArr.some((item) => { return item.code == this.userType})) {
                this.form.collegeId = response.data && response.data.deptId
              } else {
                this.form.collageId = response.data && response.data.deptId
              }
              this.form.collegeName = response.data && response.data.deptNames
            });
          }
        },
        submitForm () {
          this.$refs["form"].validate((valid) => {
            if (valid) {
              if (this.scholarshipTypeArr.some((item) => { return item.code == this.userType})) {
                editScholarshipUser(this.form).then((response) => {
                  this.$modal.msgSuccess("修改成功");
                  this.openDlg = false;
                  this.$emit("onSearch")
                });
              }
              if (this.userType == 'subsidy') {
                editPoorStudentUser({
                  registerCode: this.form.registerCode,
                  id: this.form.id,
                  diffTypeId: this.form.diffTypeId,
                  yearStr: this.form.yearStr
                }).then((response) => {
                  this.$modal.msgSuccess("修改成功");
                  this.openDlg = false;
                  this.$emit("onSearch")
                });
              }
              if (this.userType == 'schoolScholarship') {
                schoolScholarshipUserEdit({
                  registerCode: this.form.registerCode,
                  id: this.form.id,
                  yearStr: this.form.yearStr
                }).then((response) => {
                  this.$modal.msgSuccess("修改成功");
                  this.openDlg = false;
                  this.$emit("onSearch")
                });
              }
            }
          });
        },
    }

}
</script>

<style lang="scss" scoped>

</style>